package top.kuxiao.crmserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.kuxiao.crmserver.pojo.Orders;
import top.kuxiao.crmserver.service.ICustomerService;
import top.kuxiao.crmserver.service.IOrdersService;
import top.kuxiao.crmserver.utils.ResultUtil;

import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * <p>
 * 订单管理 前端控制器
 * </p>
 *
 * @author xiaozhang
 * @since 2022-01-18
 */
@Api(tags ="客户历史订单")
@CrossOrigin
@RestController
@RequestMapping("/orders")
public class OrdersController {


    @Autowired
    IOrdersService ordersService;

    @Autowired
    ICustomerService customerService;

    /**
     * 添加订单
     * */
    @ApiOperation(value = "创建订单",notes = "订单状态有：未付款、已付款、已发货、已收货、已退货")
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public ResultUtil add(Orders orders){

        orders.setOrdersDate(new Date());
        if (ordersService.save(orders)) {
            return ResultUtil.resMsgOK("添加成功");
        }
        return ResultUtil.resMsgError("添加失败");
    }

    /**
     * 查询该客户的订单
     * */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "客户的id"),
            @ApiImplicitParam(name = "page",value = "第几页")
    })
    @ApiOperation(value = "查询该客户的订单")
    @RequestMapping(value = "list/{id}",method = RequestMethod.POST)
    public ResultUtil findAll(@PathVariable("id") Integer id,@RequestParam(defaultValue = "1") int page){


        IPage<Orders> ordersPage = ordersService.queryByPage(page, 8,id);




        Map<String,Object> map=new HashMap<>();

        map.put("count",ordersPage.getPages());
        map.put("data",ordersPage.getRecords());

        return ResultUtil.resObj(map);

    }

}
